Why is this leaking memory? UIImage `cellForRowAtIndexPath:`

Posted by Emil on Stack Overflow See other posts from Stack Overflow or by Emil
Published on 2010-06-02T20:21:56Z Indexed on 2010/06/02 20:24 UTC
Read the original article Hit count: 314

Hey.

Instruments' Leaks tells me that this UIImage is leaking:

UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png", [postsArrayID objectAtIndex:indexPath.row]]]];

// If image contains anything, set cellImage to image. If image is empty, try one more time or use noImage.png, set in IB
if (image != nil){
    // If image != nil, set cellImage to that image
    cell.cellImage.image = image;
}
image = nil;
[image release];

(class cell (custom table view cell) also releases cellImage in dealloc method).

I haven't got a clue of why it's leaking, but it certainly is. The images gets loaded multiple times in a cellForRowAtIndexPath:-method. The first three cells' image does not leak (130px high, all the space avaliable).

Leaks gives me no other info than that a UIImage allocated here in the code leaks.

Can you help me figure it out? Thanks :)

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c